package com.qf.osmgrass.grass.service.Impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.core.common.PageVO;
import com.qf.common.core.entity.Grass;
import com.qf.common.core.entity.GrassLike;
import com.qf.common.core.util.UserUtils;
import com.qf.common.redis.utils.RedisUtil;
import com.qf.osmgrass.grass.mapper.GrassMapper;
import com.qf.osmgrass.grass.param.GrassParam;
import com.qf.osmgrass.grass.param.GrassSearchParam;
import com.qf.osmgrass.grass.service.GrassLikeService;
import com.qf.osmgrass.grass.service.GrassService;
import com.qf.osmgrass.grass.vo.GrassVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class GrassServiceImpl implements GrassService{

    @Resource
    private GrassMapper grassMapper;
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private GrassLikeService grassLikeService;
    @Override
    public void addGrass(GrassParam grassParam) {
        //
        Integer uid = UserUtils.getUid();
        Grass grass = new Grass();
        BeanUtils.copyProperties(grassParam,grass);
        String imgPath = String.join(",",grassParam.getImgList());
        grass.setImgPath(imgPath);
        // 对图片进行判断
        if (grassParam.getImgList().size() <= 0){
            // String imgPath = String.join(",",grassParam.getImgPath());
            grass.setImgPath("/https://pic.616pic.com/ys_bnew_img/00/04/49/vO9e6yZC89.jpg");
        }

        grass.setUid(uid);
        grass.setCheckStatus(1);
        grassMapper.insert(grass);

    }

    @Override
    public PageVO<GrassVO> grassPage(GrassSearchParam grassSearchParam) {
        Integer uid = UserUtils.getUid();
        Page<Grass> page = new Page(grassSearchParam.getPageNum(), grassSearchParam.getPageSize());
        page = grassMapper.selectPage(page, Wrappers.lambdaQuery(Grass.class)
                .eq(Grass::getCheckStatus,  1));
        List<Grass> records = page.getRecords();

        List<Integer> idList = records.stream().map(grass -> grass.getId()).collect(Collectors.toList());
        List<GrassLike> grassLikes = grassLikeService.listByGrassIdAndUid(idList, uid);


        String key = String.format("likeNum:%s", DateUtil.format(new Date(), "yyyyMMdd"));

        List<GrassVO> voList = records.stream().map(grass -> {
            GrassVO vo = new GrassVO();
            vo.setId(grass.getId());
            vo.setTitle(grass.getTitle());
            String[] split = grass.getImgPath().split(",");
            vo.setCoverImg(split[0]);

            Integer todayLikeNum = (Integer) redisUtil.getCacheMapValue(key, grass.getId().toString());
            vo.setLikeNum((todayLikeNum == null ? grass.getLikeNum():todayLikeNum + grass.getLikeNum()));

            //查询是否点赞了文章
            GrassLike findLike = grassLikes.stream().filter(grassLike -> grassLike.getGrassId().equals(grass.getId()))
                    .findFirst().orElse(null);

            vo.setLikeFlag(findLike == null ? 0:1);
            return vo;
        }).collect(Collectors.toList());
        PageVO<GrassVO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setDataList(voList);
        return pageVO;
    }
}
